import storage from 'store'
import { axios } from '@/utils/request'
import baseapi from '@/api/base'
import { getDataByParameter, saveObjetInfo, deleteObjetInfo } from '@/utils/baserequest'

const api = {
  menuServer: '/menu/',
  menus: '/menu/by/parentMenuPid/',
  menusCount: '/countBy/parentMenuPid/',
  menuByrole: '/menu/by/appIdRolePid/',
  huiwAppServer: '/appId/',
  huiwApps: '/appId/like/appInfo/',
  roleServer: '/role/',
  roles: '/role/by/orgPid/',
  rolesCount: '/role/countBy/orgPid/',
  roleMenusAdd: '/menuAuth/resourceAuths/',
  roleMenusDel: '/menuAuth/by/resourcePids/',
  roleMembersServer: '/relationRolePerson/',
  // orgPid
  roleMembers: '/relationRolePerson/by/rolePid/'
}

export default api // getMenuByParentMenu, getMenuCountByParentMenu, getMenuInfo, delMenu

// 得到角色列表
export function getRolesList (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    authOption: headers.authOption ? headers.authOption : '_1',
    org: storage.get('org')
  }
  const parameters = {
    orgPid: storage.get('org')
  }
  console.log('orgPid', storage.get('org'))
  const url = baseapi.baseUrl + baseapi.huiwcore + api.roles
  const allheaders = Object.assign(headers, appIdHeaders)
  return getDataByParameter(url, Object.assign(parameters, parameter), allheaders)
}

// 得到角色总数
// 得到下级菜单
export function getRolseCount (headers) {
  const allheaders = {
    appId: baseapi.huiwcore_app,
    authOption: '_1',
    org: storage.get('org')
  }
  return axios({
    url: baseapi.baseUrl + baseapi.huiwcore + api.rolesCount + storage.get('org'),
    method: 'get',
    headers: Object.assign(headers, allheaders),
    parameter: {}
  })
}

// 得到角色详细信息
export function getRoleInfo (parameter, headers) {
  const allheaders = {
    appId: baseapi.huiwcore_app
  }
  return axios({
    url: baseapi.baseUrl + baseapi.huiwcore + api.roleServer + parameter,
    headers: Object.assign(headers, allheaders),
    method: 'get'
    // params: parameter
  })
}

// 保存角色
// id == 0 add     post
// id != 0 update  put
export function saveRoleInfo (parameter, headers) {
  const allheaders = {
    appId: baseapi.huiwcore_app
  }
  const data = {
    orgPid: storage.get('org'),
    roleType: 'huiwRole'
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.roleServer
  return saveObjetInfo(url, Object.assign(data, parameter), Object.assign(headers, allheaders))
}

// 删除角色
export function delRole (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.roleServer
  const allheaders = Object.assign(headers, appIdHeaders)
  return deleteObjetInfo(url, parameter, allheaders)
}

// 得到appId 列表
export function getHuiwApps (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    authOption: '1'
  }
  const parameters = {
    appInfo: ' ',
    pageSize: 100000
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.huiwApps
  const allheaders = Object.assign(headers, appIdHeaders)
  console.log(url, parameters, allheaders)
  return getDataByParameter(url, parameters, allheaders)
}

// 得到下级菜单
export function getMenuByParentMenu (parentMenuPid, appId, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: '',
    authOption: '__1'
  }
  const parameters = {
    parentMenuPid: parentMenuPid,
    appId: appId,
    pageSize: 100000
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.menus
  const allheaders = Object.assign(headers, appIdHeaders)
  return getDataByParameter(url, parameters, allheaders)
  // .then(res => {
  //   return res
  // })
}

// 得到下级菜单
export function getMenuCountByParentMenu (parentMenuPid, appId, headers) {
  const allheaders = {
    appId: baseapi.huiwcore_app
  }
  return axios({
    url: baseapi.baseUrl + baseapi.huiwcore + api.menusCount + appId + '/' + parentMenuPid,
    method: 'get',
    headers: Object.assign(headers, allheaders),
    parameter: {}
  })
}

// 得到菜单详细信息
export function getMenuInfo (parameter, headers) {
  const allheaders = {
    appId: baseapi.huiwcore_app
  }
  return axios({
    url: baseapi.baseUrl + baseapi.huiwcore + api.menuServer + parameter,
    headers: Object.assign(headers, allheaders),
    method: 'get'
    // params: parameter
  })
}

// 删除菜单
export function delMenu (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.menuServer
  const allheaders = Object.assign(headers, appIdHeaders)
  return deleteObjetInfo(url, parameter, allheaders)
}

// 查询角色拥有的菜单权限
export function getMenuByrole (appId, rolePid, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    org: storage.get('org')
  }
  const parameters = {
    appId: appId,
    rolePid: rolePid,
    pageSize: 1000000
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.menuByrole
  const allheaders = Object.assign(headers, appIdHeaders)
  return getDataByParameter(url, parameters, allheaders)
}

// 删除角色拥有的菜单权限
export function delMenuByrole (parameters, headers) {
  if (parameters.resourceAuths && parameters.resourceAuths.length > 0) {
  } else {
    return
  }
  if (parameters.authOpts && (parameters.authOpts === '1' || parameters.authOpts === '__1')) {
    if (parameters.authOpts === '1') {
      parameters.authOpts = 1
    }
    if (parameters.authOpts === '__1') {
      parameters.authOpts = 3
    }
  } else {
    return
  }
  const appIdHeaders = {
    appId: baseapi.huiwcore_app
  }
  parameters.resourceAuths = parameters.resourceAuths.toString()
  const url = baseapi.baseUrl + baseapi.huiwcore + api.roleMenusDel
  const allheaders = Object.assign(headers, appIdHeaders)
  return deleteObjetInfo(url, parameters, allheaders)
}

// 添加角色拥有的菜单权限
export function addMenuByrole (parameters, headers) {
  if (parameters.resourceAuths && parameters.resourceAuths.length > 0) {
  } else {
    return
  }
  const appIdHeaders = {
    appId: baseapi.huiwcore_app
  }
  parameters.resourceAuths = parameters.resourceAuths.toString()
  const url = baseapi.baseUrl + baseapi.huiwcore + api.roleMenusAdd
  const allheaders = Object.assign(headers, appIdHeaders)
  return saveObjetInfo(url, parameters, allheaders)
}

// 查询角色关联的成员
export function getRoleMembersByRolePid (rolePid, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app,
    authOption: '1'
  }
  const parameters = {
    rolePid: rolePid,
    pageSize: 1000000
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.roleMembers
  const allheaders = Object.assign(headers, appIdHeaders)
  return getDataByParameter(url, parameters, allheaders)
}

// 添加角色成员
export function addRoleMember (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app
  }
  const data = {
    sysSeq: 0,
    pid: '',
    orgPid: storage.get('org')
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.roleMembersServer
  const allheaders = Object.assign(headers, appIdHeaders)
  return saveObjetInfo(url, Object.assign(parameter, data), allheaders)
}

// 删除角色成员
export function delRoleMember (parameter, headers) {
  const appIdHeaders = {
    appId: baseapi.huiwcore_app
  }
  const data = {
    orgPid: storage.get('org')
  }
  const url = baseapi.baseUrl + baseapi.huiwcore + api.roleMembersServer // + '/'
  const allheaders = Object.assign(headers, appIdHeaders)
  return deleteObjetInfo(url, Object.assign(parameter, data), allheaders)
}
